﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.Model.Core
{
    public class StorageHandler
    {
        protected Dictionary<Ingredient, int> storage;
        public StorageHandler()
        {
            storage = new Dictionary<Ingredient, int>();
        }
        public StorageHandler Clone()
        {
            var newHandler = new StorageHandler();
            //shallow copy: keys (ingredients) refer to the same objects
            //values are primitive value types, they are copied
            newHandler.storage = storage.ToDictionary(
                kvp => kvp.Key, kvp => kvp.Value);
            return newHandler;
        }
        public int ChangeAmount(Ingredient ingredient, int diff)
        {
            int newAmount = storage[ingredient] + diff;
            SetAmount(ingredient, amount: newAmount);
            return newAmount;
        }
        public void SetAmount(Ingredient ingredient, int amount)
        {
            //TODO negative exception
            storage[ingredient] = amount;
        }
        public int GetAmount(Ingredient ingredient)
        {
            //TODO exception if not existing
            return storage[ingredient];
        }
        public void RegisterDiff(StorageHandler referenceStorage)
        {
            foreach (var kvp in referenceStorage.storage)
            {
                SetAmount(ingredient: kvp.Key, amount: kvp.Value);
            }
        }
    }
}
